package com.hrg.rds.biz.comm;

import cn.hutool.json.JSONUtil;
import com.hrg.rds.biz.config.RdsConfiguration;
import com.hrg.seer.sdk.rbk.RbkResult;
import com.hrg.seer.sdk.rbk.RbkResultKind;
import com.hrg.seer.sdk.rbk.api.NavApiEnum;
import com.hrg.seer.sdk.rbk.msg.BaseRes;
import com.hrg.seer.sdk.rbk.msg.nav.BaseNav;
import com.hrg.seer.sdk.rbk.msg.nav.GoTargetListReq;
import com.hrg.seer.sdk.rbk.msg.nav.TargetPathReq;
import com.hrg.seer.sdk.rbk.msg.nav.TargetPathRes;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
public class NavService {

	// 暂停导航
	private static RbkResult ROBOT_TASK_PAUSE_REQ(String ip) {
		int apiNo = NavApiEnum.ROBOT_TASK_PAUSE_REQ.getApiNo();
		return RdsConfiguration.AgvIpClientMap.get(ip).request(apiNo, null);
	}

	public static boolean taskPause(String ip) {
		RbkResult rbkResult = NavService.ROBOT_TASK_PAUSE_REQ(ip);
		if (rbkResult.getKind().equals(RbkResultKind.Ok)) {
			BaseRes res = JSONUtil.toBean(rbkResult.getResStr(), BaseRes.class);
			if (res.getRet_code() == 0) {
				log.info("暂停导航成功 ip = {}", ip);
				return true;
			}
			log.error("暂停导航失败 ret_code = {}, err_msg = {}，ip = {}", res.getRet_code(), res.getErr_msg(), ip);
		}
		log.error("暂停导航失败 kind = {}，ip = {}", rbkResult.getKind().name(), ip);
		return false;
	}

	// 恢复导航
	private static RbkResult ROBOT_TASK_RESUME_REQ(String ip) {
		int apiNo = NavApiEnum.ROBOT_TASK_RESUME_REQ.getApiNo();
		return RdsConfiguration.AgvIpClientMap.get(ip).request(apiNo, null);
	}

	public static boolean taskResume(String ip) {
		RbkResult rbkResult = NavService.ROBOT_TASK_RESUME_REQ(ip);
		if (rbkResult.getKind().equals(RbkResultKind.Ok)) {
			BaseRes res = JSONUtil.toBean(rbkResult.getResStr(), BaseRes.class);
			if (res.getRet_code() == 0) {
				log.info("恢复导航成功 ip = {}", ip);
				return true;
			}
			log.error("恢复导航失败 ret_code = {}, err_msg = {}，ip = {}", res.getRet_code(), res.getErr_msg(), ip);
		}
		log.error("恢复导航失败 kind = {}，ip = {}", rbkResult.getKind().name(), ip);
		return false;
	}

	// 取消导航
	private static RbkResult ROBOT_TASK_CANCEL_REQ(String ip) {
		int apiNo = NavApiEnum.ROBOT_TASK_CANCEL_REQ.getApiNo();
		return RdsConfiguration.AgvIpClientMap.get(ip).request(apiNo, null);
	}

	public static boolean taskCancel(String ip) {
		RbkResult rbkResult = NavService.ROBOT_TASK_CANCEL_REQ(ip);
		if (rbkResult.getKind().equals(RbkResultKind.Ok)) {
			BaseRes res = JSONUtil.toBean(rbkResult.getResStr(), BaseRes.class);
			if (res.getRet_code() == 0) {
				log.info("取消导航成功 ip = {}", ip);
				return true;
			}
			log.error("取消导航失败 ret_code = {}, err_msg = {}，ip = {}", res.getRet_code(), res.getErr_msg(), ip);
		}
		log.error("取消导航失败 kind = {}，ip = {}", rbkResult.getKind().name(), ip);
		return false;
	}

	// 获取路径导航的路径
	private static RbkResult ROBOT_TASK_TARGET_PATH_REQ(String ip, String toStation) {
		int apiNo = NavApiEnum.ROBOT_TASK_TARGET_PATH_REQ.getApiNo();
		TargetPathReq req = TargetPathReq.builder().id(toStation).build();
		return RdsConfiguration.AgvIpClientMap.get(ip).request(apiNo, JSONUtil.toJsonStr(req));
	}

	public static List<String> getNavPath(String ip, String currentStation, String targetStation) {
		RbkResult rbkResult = NavService.ROBOT_TASK_TARGET_PATH_REQ(ip, targetStation);
		if (rbkResult.getKind().equals(RbkResultKind.Ok)) {
			TargetPathRes res = JSONUtil.toBean(rbkResult.getResStr(), TargetPathRes.class);
			if (res.getRet_code() == 0) {
				List<String> path = res.getPath();
				log.info("获取路径成功 ip = {}, currentStation = {}, targetStation = {}, path = {}", ip, currentStation, targetStation, path);
				return path;
			}
			log.error("获取路径失败 ret_code = {}, err_msg = {}，ip = {}, currentStation = {}, targetStation = {}", res.getRet_code(), res.getErr_msg(), ip, currentStation, targetStation);
		}
		log.error("获取路径失败 kind = {}，ip = {}, currentStation = {}, targetStation = {}", rbkResult.getKind().name(), ip, currentStation, targetStation);
		return null;
	}

	public static int getNavPathCnt(String ip, String currentStation, String keyStation) {
		RbkResult rbkResult = NavService.ROBOT_TASK_TARGET_PATH_REQ(ip, keyStation);
		if (rbkResult.getKind().equals(RbkResultKind.Ok)) {
			TargetPathRes res = JSONUtil.toBean(rbkResult.getResStr(), TargetPathRes.class);
			if (res.getRet_code() == 0) {
				int cnt = res.getPath().size();
				log.info("获取路径成功 cnt = {}，ip = {}, currentStation = {}, keyStation = {}, path = {}", cnt, ip, currentStation, keyStation, res.getPath());
				return cnt;
			}
			log.error("获取路径失败 ret_code = {}, err_msg = {}，ip = {}, currentStation = {}, keyStation = {}", res.getRet_code(), res.getErr_msg(), ip, currentStation, keyStation);
		}
		log.error("获取路径失败 kind = {}，ip = {}, currentStation = {}, keyStation = {}", rbkResult.getKind().name(), ip, currentStation, keyStation);
		return Integer.MAX_VALUE;
	}

	private static RbkResult ROBOT_TASK_GOTARGETLIST_REQ(String ip, List<BaseNav> navs) {
		int apiNo = NavApiEnum.ROBOT_TASK_GOTARGETLIST_REQ.getApiNo();
		GoTargetListReq req = GoTargetListReq.builder().move_task_list(navs).build();
		return RdsConfiguration.AgvIpClientMap.get(ip).request(apiNo, JSONUtil.toJsonStr(req));
	}

	public static boolean goTargetList(String ip, List<BaseNav> navs) {
		RbkResult rbkResult = NavService.ROBOT_TASK_GOTARGETLIST_REQ(ip, navs);
		if (rbkResult.getKind().equals(RbkResultKind.Ok)) {
			BaseRes res = JSONUtil.toBean(rbkResult.getResStr(), TargetPathRes.class);
			if (res.getRet_code() == 0) {
				log.info("指定路径导航成功 ip = {}", ip);
				return true;
			}
			log.error("指定路径导航失败 ret_code = {}, err_msg = {}，ip = {}", res.getRet_code(), res.getErr_msg(), ip);
		}
		log.error("指定路径导航失败 kind = {}，ip = {}", rbkResult.getKind().name(), ip);
		return false;
	}

	private static RbkResult ROBOT_TASK_CLEARTARGETLIST_REQ(String ip) {
		int apiNo = NavApiEnum.ROBOT_TASK_CLEARTARGETLIST_REQ.getApiNo();
		return RdsConfiguration.AgvIpClientMap.get(ip).request(apiNo, null);
	}

	public static boolean clearTargetList(String ip) {
		RbkResult rbkResult = NavService.ROBOT_TASK_CLEARTARGETLIST_REQ(ip);
		if (rbkResult.getKind().equals(RbkResultKind.Ok)) {
			BaseRes res = JSONUtil.toBean(rbkResult.getResStr(), TargetPathRes.class);
			if (res.getRet_code() == 0) {
				log.info("清除指定路径导航成功 ip = {}", ip);
				return true;
			}
			log.error("清除指定路径导航失败 ret_code = {}, err_msg = {}，ip = {}", res.getRet_code(), res.getErr_msg(), ip);
		}
		log.error("清除指定路径导航失败 kind = {}，ip = {}", rbkResult.getKind().name(), ip);
		return false;
	}
}
